{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a24201eb-2541-4ef1-bed2-14f34fe37ae7",
   "metadata": {},
   "source": [
    "# Distributed/Fault Tolerant Agents with Burr"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2055510a-1b2a-4deb-8896-0f65e6a773dd",
   "metadata": {},
   "source": [
    "This is supporting code for two blog posts:\n",
    "1. [Parallel Multi Agent Workflows with Burr](https://blog.dagworks.io/p/93838d1f-52b5-4a72-999f-9cab9733d4fe)\n",
    "2. [Parallel, Fault-Tolerant Agents with Burr/Ray](https://blog.dagworks.io/p/5baf1077-2490-44bc-afff-fcdafe18e819)\n",
    "\n",
    "This runs the application using a local ray instance."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "37a24484-1c2c-4e63-b45f-bb35a7c7151e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import ray\n",
    "import pprint\n",
    "\n",
    "import application as parallel_application\n",
    "from burr.core import State"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "62ed5b17-9795-4426-85c1-a3384bb7d25f",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: burr in /Users/elijahbenizzy/.pyenv/versions/3.12.0/envs/burr-3-12/lib/python3.12/site-packages (0.32.0)\n",
      "Requirement already satisfied: openai in /Users/elijahbenizzy/.pyenv/versions/3.12.0/envs/burr-3-12/lib/python3.12/site-packages (1.47.1)\n",
      "Requirement already satisfied: ray in /Users/elijahbenizzy/.pyenv/versions/3.12.0/envs/burr-3-12/lib/python3.12/site-packages (2.40.0)\n",
      "Requirement already satisfied: anyio<5,>=3.5.0 in /Users/elijahbenizzy/.pyenv/versions/3.12.0/envs/burr-3-12/lib/python3.12/site-packages (from openai) (4.4.0)\n",
      "Requirement already satisfied: distro<2,>=1.7.0 in /Users/elijahbenizzy/.pyenv/versions/3.12.0/envs/burr-3-12/lib/python3.12/site-packages (from openai) (1.9.0)\n",
      "Requirement already satisfied: httpx<1,>=0.23.0 in /Users/elijahbenizzy/.pyenv/versions/3.12.0/envs/burr-3-12/lib/python3.12/site-packages (from openai) (0.27.0)\n",
      "Requirement already satisfied: jiter<1,>=0.4.0 in /Users/elijahbenizzy/.pyenv/versions/3.12.0/envs/burr-3-12/lib/python3.12/site-packages (from openai) (0.5.0)\n",
      "Requirement already satisfied: pydantic<3,>=1.9.0 in /Users/elijahbenizzy/.pyenv/versions/3.12.0/envs/burr-3-12/lib/python3.12/site-packages (from openai) (2.8.2)\n",
      "Requirement already satisfied: sniffio in /Users/elijahbenizzy/.pyenv/versions/3.12.0/envs/burr-3-12/lib/python3.12/site-packages (from openai) (1.3.1)\n",
      "Requirement already satisfied: tqdm>4 in /Users/elijahbenizzy/.pyenv/versions/3.12.0/envs/burr-3-12/lib/python3.12/site-packages (from openai) (4.66.4)\n",
      "Requirement already satisfied: typing-extensions<5,>=4.11 in /Users/elijahbenizzy/.pyenv/versions/3.12.0/envs/burr-3-12/lib/python3.12/site-packages (from openai) (4.12.2)\n",
      "Requirement already satisfied: click>=7.0 in /Users/elijahbenizzy/.pyenv/versions/3.12.0/envs/burr-3-12/lib/python3.12/site-packages (from ray) (8.1.7)\n",
      "Requirement already satisfied: filelock in /Users/elijahbenizzy/.pyenv/versions/3.12.0/envs/burr-3-12/lib/python3.12/site-packages (from ray) (3.15.4)\n",
      "Requirement already satisfied: jsonschema in /Users/elijahbenizzy/.pyenv/versions/3.12.0/envs/burr-3-12/lib/python3.12/site-packages (from ray) (4.23.0)\n",
      "Requirement already satisfied: msgpack<2.0.0,>=1.0.0 in /Users/elijahbenizzy/.pyenv/versions/3.12.0/envs/burr-3-12/lib/python3.12/site-packages (from ray) (1.0.8)\n",
      "Requirement already satisfied: packaging in /Users/elijahbenizzy/.pyenv/versions/3.12.0/envs/burr-3-12/lib/python3.12/site-packages (from ray) (24.1)\n",
      "Requirement already satisfied: protobuf!=3.19.5,>=3.15.3 in /Users/elijahbenizzy/.pyenv/versions/3.12.0/envs/burr-3-12/lib/python3.12/site-packages (from ray) (4.25.4)\n",
      "Requirement already satisfied: pyyaml in /Users/elijahbenizzy/.pyenv/versions/3.12.0/envs/burr-3-12/lib/python3.12/site-packages (from ray) (6.0.1)\n",
      "Requirement already satisfied: aiosignal in /Users/elijahbenizzy/.pyenv/versions/3.12.0/envs/burr-3-12/lib/python3.12/site-packages (from ray) (1.3.1)\n",
      "Requirement already satisfied: frozenlist in /Users/elijahbenizzy/.pyenv/versions/3.12.0/envs/burr-3-12/lib/python3.12/site-packages (from ray) (1.4.1)\n",
      "Requirement already satisfied: requests in /Users/elijahbenizzy/.pyenv/versions/3.12.0/envs/burr-3-12/lib/python3.12/site-packages (from ray) (2.32.3)\n",
      "Requirement already satisfied: idna>=2.8 in /Users/elijahbenizzy/.pyenv/versions/3.12.0/envs/burr-3-12/lib/python3.12/site-packages (from anyio<5,>=3.5.0->openai) (3.7)\n",
      "Requirement already satisfied: certifi in /Users/elijahbenizzy/.pyenv/versions/3.12.0/envs/burr-3-12/lib/python3.12/site-packages (from httpx<1,>=0.23.0->openai) (2024.7.4)\n",
      "Requirement already satisfied: httpcore==1.* in /Users/elijahbenizzy/.pyenv/versions/3.12.0/envs/burr-3-12/lib/python3.12/site-packages (from httpx<1,>=0.23.0->openai) (1.0.5)\n",
      "Requirement already satisfied: h11<0.15,>=0.13 in /Users/elijahbenizzy/.pyenv/versions/3.12.0/envs/burr-3-12/lib/python3.12/site-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai) (0.14.0)\n",
      "Requirement already satisfied: annotated-types>=0.4.0 in /Users/elijahbenizzy/.pyenv/versions/3.12.0/envs/burr-3-12/lib/python3.12/site-packages (from pydantic<3,>=1.9.0->openai) (0.7.0)\n",
      "Requirement already satisfied: pydantic-core==2.20.1 in /Users/elijahbenizzy/.pyenv/versions/3.12.0/envs/burr-3-12/lib/python3.12/site-packages (from pydantic<3,>=1.9.0->openai) (2.20.1)\n",
      "Requirement already satisfied: attrs>=22.2.0 in /Users/elijahbenizzy/.pyenv/versions/3.12.0/envs/burr-3-12/lib/python3.12/site-packages (from jsonschema->ray) (23.2.0)\n",
      "Requirement already satisfied: jsonschema-specifications>=2023.03.6 in /Users/elijahbenizzy/.pyenv/versions/3.12.0/envs/burr-3-12/lib/python3.12/site-packages (from jsonschema->ray) (2023.12.1)\n",
      "Requirement already satisfied: referencing>=0.28.4 in /Users/elijahbenizzy/.pyenv/versions/3.12.0/envs/burr-3-12/lib/python3.12/site-packages (from jsonschema->ray) (0.35.1)\n",
      "Requirement already satisfied: rpds-py>=0.7.1 in /Users/elijahbenizzy/.pyenv/versions/3.12.0/envs/burr-3-12/lib/python3.12/site-packages (from jsonschema->ray) (0.19.1)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in /Users/elijahbenizzy/.pyenv/versions/3.12.0/envs/burr-3-12/lib/python3.12/site-packages (from requests->ray) (3.3.2)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/elijahbenizzy/.pyenv/versions/3.12.0/envs/burr-3-12/lib/python3.12/site-packages (from requests->ray) (2.2.2)\n",
      "\n",
      "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.2.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.3.1\u001b[0m\n",
      "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpython3.12 -m pip install --upgrade pip\u001b[0m\n",
      "Note: you may need to restart the kernel to use updated packages.\n",
      "The burr.integrations.notebook extension is already loaded. To reload it, use:\n",
      "  %reload_ext burr.integrations.notebook\n"
     ]
    }
   ],
   "source": [
    "# execute to load the Burr and Hamilton extensions\n",
    "%pip install burr openai ray\n",
    "%load_ext burr.integrations.notebook"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "eb838d9c-ff30-4bc2-82b6-79b571c5b11c",
   "metadata": {},
   "outputs": [],
   "source": [
    "app = parallel_application.application()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d693522a-fd47-4019-8935-6a38fcf46ebf",
   "metadata": {},
   "source": [
    "# Application Graph\n",
    "\n",
    "This is the flowchart of the application. Note that `generate_all_poems` is actually a recursive set of sub-applications."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "3b6335ea-8f1d-48fb-b74a-4fea033b360f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 12.0.0 (20240704.0754)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"445pt\" height=\"241pt\"\n",
       " viewBox=\"0.00 0.00 444.85 241.40\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 237.4)\">\n",
       "<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-237.4 440.85,-237.4 440.85,4 -4,4\"/>\n",
       "<!-- user_input -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>user_input</title>\n",
       "<path fill=\"#b4d8e4\" stroke=\"black\" d=\"M253.85,-167.8C253.85,-167.8 193.25,-167.8 193.25,-167.8 187.25,-167.8 181.25,-161.8 181.25,-155.8 181.25,-155.8 181.25,-143.2 181.25,-143.2 181.25,-137.2 187.25,-131.2 193.25,-131.2 193.25,-131.2 253.85,-131.2 253.85,-131.2 259.85,-131.2 265.85,-137.2 265.85,-143.2 265.85,-143.2 265.85,-155.8 265.85,-155.8 265.85,-161.8 259.85,-167.8 253.85,-167.8\"/>\n",
       "<text text-anchor=\"middle\" x=\"223.55\" y=\"-143.7\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">user_input</text>\n",
       "</g>\n",
       "<!-- generate_all_poems -->\n",
       "<g id=\"node6\" class=\"node\">\n",
       "<title>generate_all_poems</title>\n",
       "<path fill=\"#b4d8e4\" stroke=\"black\" d=\"M283.85,-102.2C283.85,-102.2 163.25,-102.2 163.25,-102.2 157.25,-102.2 151.25,-96.2 151.25,-90.2 151.25,-90.2 151.25,-77.6 151.25,-77.6 151.25,-71.6 157.25,-65.6 163.25,-65.6 163.25,-65.6 283.85,-65.6 283.85,-65.6 289.85,-65.6 295.85,-71.6 295.85,-77.6 295.85,-77.6 295.85,-90.2 295.85,-90.2 295.85,-96.2 289.85,-102.2 283.85,-102.2\"/>\n",
       "<text text-anchor=\"middle\" x=\"223.55\" y=\"-78.1\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">generate_all_poems</text>\n",
       "</g>\n",
       "<!-- user_input&#45;&gt;generate_all_poems -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>user_input&#45;&gt;generate_all_poems</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M223.55,-130.78C223.55,-125.55 223.55,-119.67 223.55,-113.92\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"227.05,-114.03 223.55,-104.03 220.05,-114.03 227.05,-114.03\"/>\n",
       "</g>\n",
       "<!-- input__poem_subject -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>input__poem_subject</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"143.1,-233.4 0,-233.4 0,-196.8 143.1,-196.8 143.1,-233.4\"/>\n",
       "<text text-anchor=\"middle\" x=\"71.55\" y=\"-209.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">input: poem_subject</text>\n",
       "</g>\n",
       "<!-- input__poem_subject&#45;&gt;user_input -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>input__poem_subject&#45;&gt;user_input</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M113.89,-196.38C131.52,-189.01 152.18,-180.36 170.79,-172.58\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"171.9,-175.9 179.78,-168.82 169.2,-169.45 171.9,-175.9\"/>\n",
       "</g>\n",
       "<!-- input__max_drafts -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>input__max_drafts</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"286.48,-233.4 160.63,-233.4 160.63,-196.8 286.48,-196.8 286.48,-233.4\"/>\n",
       "<text text-anchor=\"middle\" x=\"223.55\" y=\"-209.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">input: max_drafts</text>\n",
       "</g>\n",
       "<!-- input__max_drafts&#45;&gt;user_input -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>input__max_drafts&#45;&gt;user_input</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M223.55,-196.38C223.55,-191.15 223.55,-185.27 223.55,-179.52\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"227.05,-179.63 223.55,-169.63 220.05,-179.63 227.05,-179.63\"/>\n",
       "</g>\n",
       "<!-- input__poem_types -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>input__poem_types</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"436.85,-233.4 304.25,-233.4 304.25,-196.8 436.85,-196.8 436.85,-233.4\"/>\n",
       "<text text-anchor=\"middle\" x=\"370.55\" y=\"-209.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">input: poem_types</text>\n",
       "</g>\n",
       "<!-- input__poem_types&#45;&gt;user_input -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>input__poem_types&#45;&gt;user_input</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M329.6,-196.38C312.71,-189.07 292.94,-180.52 275.08,-172.79\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"276.49,-169.59 265.92,-168.83 273.71,-176.02 276.49,-169.59\"/>\n",
       "</g>\n",
       "<!-- final_results -->\n",
       "<g id=\"node5\" class=\"node\">\n",
       "<title>final_results</title>\n",
       "<path fill=\"#b4d8e4\" stroke=\"black\" d=\"M258.35,-36.6C258.35,-36.6 188.75,-36.6 188.75,-36.6 182.75,-36.6 176.75,-30.6 176.75,-24.6 176.75,-24.6 176.75,-12 176.75,-12 176.75,-6 182.75,0 188.75,0 188.75,0 258.35,0 258.35,0 264.35,0 270.35,-6 270.35,-12 270.35,-12 270.35,-24.6 270.35,-24.6 270.35,-30.6 264.35,-36.6 258.35,-36.6\"/>\n",
       "<text text-anchor=\"middle\" x=\"223.55\" y=\"-12.5\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">final_results</text>\n",
       "</g>\n",
       "<!-- generate_all_poems&#45;&gt;final_results -->\n",
       "<g id=\"edge5\" class=\"edge\">\n",
       "<title>generate_all_poems&#45;&gt;final_results</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M223.55,-65.18C223.55,-59.95 223.55,-54.07 223.55,-48.32\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"227.05,-48.43 223.55,-38.43 220.05,-48.43 227.05,-48.43\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "ApplicationGraph(actions=[user_input: {} -> max_drafts, poem_types, poem_subject, final_results: proposals, poem_types -> final_results, generate_all_poems: poem_types, poem_subject, max_drafts -> proposals], transitions=[Transition(from_=user_input: {} -> max_drafts, poem_types, poem_subject, to=generate_all_poems: poem_types, poem_subject, max_drafts -> proposals, condition=condition: default), Transition(from_=generate_all_poems: poem_types, poem_subject, max_drafts -> proposals, to=final_results: proposals, poem_types -> final_results, condition=condition: default)], entrypoint=user_input: {} -> max_drafts, poem_types, poem_subject)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "app.graph"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "81209a79-aa12-42ba-bfe0-468be5d2cacf",
   "metadata": {},
   "source": [
    "# Sub-application Graph\n",
    "\n",
    "This represents the sub-application inside `generate_all_poems` -- we generate one of these for each poem type specified by the user (E.G. limerick, haiku, sonnet, etc...)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "58bd3d4d-70b8-42bc-b520-6d5bb95e8575",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 12.0.0 (20240704.0754)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"233pt\" height=\"195pt\"\n",
       " viewBox=\"0.00 0.00 233.00 195.30\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 191.3)\">\n",
       "<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-191.3 229,-191.3 229,4 -4,4\"/>\n",
       "<!-- edit -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>edit</title>\n",
       "<path fill=\"#b4d8e4\" stroke=\"black\" d=\"M42,-187.3C42,-187.3 12,-187.3 12,-187.3 6,-187.3 0,-181.3 0,-175.3 0,-175.3 0,-162.7 0,-162.7 0,-156.7 6,-150.7 12,-150.7 12,-150.7 42,-150.7 42,-150.7 48,-150.7 54,-156.7 54,-162.7 54,-162.7 54,-175.3 54,-175.3 54,-181.3 48,-187.3 42,-187.3\"/>\n",
       "<text text-anchor=\"middle\" x=\"27\" y=\"-163.2\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">edit</text>\n",
       "</g>\n",
       "<!-- write -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>write</title>\n",
       "<path fill=\"#b4d8e4\" stroke=\"black\" d=\"M42,-104.2C42,-104.2 12,-104.2 12,-104.2 6,-104.2 0,-98.2 0,-92.2 0,-92.2 0,-79.6 0,-79.6 0,-73.6 6,-67.6 12,-67.6 12,-67.6 42,-67.6 42,-67.6 48,-67.6 54,-73.6 54,-79.6 54,-79.6 54,-92.2 54,-92.2 54,-98.2 48,-104.2 42,-104.2\"/>\n",
       "<text text-anchor=\"middle\" x=\"27\" y=\"-80.1\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">write</text>\n",
       "</g>\n",
       "<!-- edit&#45;&gt;write -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>edit&#45;&gt;write</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M24.41,-150.27C23.82,-145.54 23.29,-140.44 23,-135.7 22.55,-128.38 22.55,-126.52 23,-119.2 23.06,-118.16 23.14,-117.11 23.22,-116.05\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"26.7,-116.45 24.25,-106.14 19.74,-115.73 26.7,-116.45\"/>\n",
       "</g>\n",
       "<!-- final_draft -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>final_draft</title>\n",
       "<path fill=\"#b4d8e4\" stroke=\"black\" d=\"M105.42,-36.6C105.42,-36.6 48.58,-36.6 48.58,-36.6 42.58,-36.6 36.58,-30.6 36.58,-24.6 36.58,-24.6 36.58,-12 36.58,-12 36.58,-6 42.58,0 48.58,0 48.58,0 105.42,0 105.42,0 111.42,0 117.42,-6 117.42,-12 117.42,-12 117.42,-24.6 117.42,-24.6 117.42,-30.6 111.42,-36.6 105.42,-36.6\"/>\n",
       "<text text-anchor=\"middle\" x=\"77\" y=\"-12.5\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">final_draft</text>\n",
       "</g>\n",
       "<!-- edit&#45;&gt;final_draft -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>edit&#45;&gt;final_draft</title>\n",
       "<path fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" d=\"M54.47,-165.7C74.35,-162.44 99.92,-154.58 113,-135.7 131.82,-108.53 114.25,-71.16 97.71,-46.17\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"100.72,-44.36 92.13,-38.16 94.97,-48.37 100.72,-44.36\"/>\n",
       "<text text-anchor=\"middle\" x=\"172.5\" y=\"-80.85\" font-family=\"Times,serif\" font-size=\"14.00\">len(feedback) == 0</text>\n",
       "</g>\n",
       "<!-- write&#45;&gt;edit -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>write&#45;&gt;edit</title>\n",
       "<path fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" d=\"M27,-104.46C27,-114.55 27,-127.48 27,-139.03\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"23.5,-138.89 27,-148.89 30.5,-138.89 23.5,-138.89\"/>\n",
       "<text text-anchor=\"middle\" x=\"69.38\" y=\"-122.4\" font-family=\"Times,serif\" font-size=\"14.00\">num_drafts &lt; 2</text>\n",
       "</g>\n",
       "<!-- write&#45;&gt;final_draft -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>write&#45;&gt;final_draft</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M40.4,-67.32C45.44,-60.7 51.3,-53.02 56.82,-45.77\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"59.37,-48.21 62.65,-38.13 53.8,-43.96 59.37,-48.21\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "Graph(actions=[edit: current_draft, poem_type, poem_subject -> feedback, write: feedback, current_draft, poem_type, poem_subject -> current_draft, draft_history, num_drafts, final_draft: current_draft -> final_draft], transitions=[Transition(from_=write: feedback, current_draft, poem_type, poem_subject -> current_draft, draft_history, num_drafts, to=edit: current_draft, poem_type, poem_subject -> feedback, condition=condition: num_drafts < 2), Transition(from_=write: feedback, current_draft, poem_type, poem_subject -> current_draft, draft_history, num_drafts, to=final_draft: current_draft -> final_draft, condition=condition: default), Transition(from_=edit: current_draft, poem_type, poem_subject -> feedback, to=final_draft: current_draft -> final_draft, condition=condition: len(feedback) == 0), Transition(from_=edit: current_draft, poem_type, poem_subject -> feedback, to=write: feedback, current_draft, poem_type, poem_subject -> current_draft, draft_history, num_drafts, condition=condition: default)])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Do not access the sub-application this way -- just for the demo!\n",
    "app.graph.get_action(\"generate_all_poems\").action(state=State({\"max_drafts\" : 2}), inputs={}).graph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "60988516-1d20-40e6-a484-214f3ee86383",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "        <iframe\n",
       "            width=\"100%\"\n",
       "            height=\"400\"\n",
       "            src=\"http://127.0.0.1:7241\"\n",
       "            frameborder=\"0\"\n",
       "            allowfullscreen\n",
       "            \n",
       "        ></iframe>\n",
       "        "
      ],
      "text/plain": [
       "<IPython.lib.display.IFrame at 0x132046bd0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# execute cell to launch the UI\n",
    "%burr_ui"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "3573dad5-bb92-47c3-9496-230ab6fd9354",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2024-12-22 20:40:19,330\tINFO worker.py:1654 -- Calling ray.init() again after it has already been called.\n",
      "This is trying to return without having computed a single action -- we'll end up just returning some Nones. This means that nothing was executed (E.G. that the state machine had nowhere to go). Either fix the state machine orthe halt conditions, or both... Halt conditions are: halt_before=[], halt_after=['final_results'].Note that this is considered undefined behavior -- if you get here, you should fix!\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'__PRIOR_STEP': 'final_results',\n",
      " '__SEQUENCE_ID': 3,\n",
      " 'final_results': 'sonnet:\\n'\n",
      "                  \"['In myriad states, a cyclic dance unfolds,  \\\\nUpon the \"\n",
      "                  'stage of logic, firm and clear.  \\\\nAn automaton with '\n",
      "                  'structured bounds it holds,  \\\\nDesigned by minds, both '\n",
      "                  'intricate and near.  \\\\n\\\\nWith each transition, patterns '\n",
      "                  'trace the flow,  \\\\nA pulse of truth propels the forward '\n",
      "                  'sweep.  \\\\nConditions mark the paths, both high and low,  '\n",
      "                  '\\\\nAs inputs carve formations, wide and deep.  '\n",
      "                  '\\\\n\\\\nDeterministic threads, like textiles spun,  \\\\nWeave '\n",
      "                  'tapestries of boundless, coded choice.  \\\\nA binary ballet '\n",
      "                  'that shies from none,  \\\\nYet whispers softly with a silent '\n",
      "                  'voice.  \\\\n\\\\nIn finite bounds, infinite patterns weave,  '\n",
      "                  '\\\\nOrder from chaos, logic born to leave.  \\', \"In a realm '\n",
      "                  'where nodes intertwine,  \\\\nDwells the dream of a state '\n",
      "                  \"machine's line.  \\\\nWith transitions so neat,  \\\\nA puzzle \"\n",
      "                  'complete,  \\\\nIt dances through states, a design.\", \"Beads '\n",
      "                  'of states align,  \\\\nSilent rhythms weave their dance—  '\n",
      "                  '\\\\nLogic\\'s steady hand.\", \\'Silent gears in whispered '\n",
      "                  'dance engage,  \\\\nTransitions weave through every stage.  '\n",
      "                  '\\\\nAbstract notions structured with flair,  \\\\nTapestries '\n",
      "                  'of circuits, precise and rare.  \\\\nEach decision flows with '\n",
      "                  'calculated grace,  \\\\n\\\\nMachines hum softly, hidden in '\n",
      "                  'embrace.  \\\\nAs states shift, each holds its refrain,  '\n",
      "                  '\\\\nChoreographed steps in methodical chain.  \\\\nHarmony '\n",
      "                  'thrives in digital cadence set,  \\\\nIn vivid lines of '\n",
      "                  'binary duet.  \\\\nNodes in purpose, connections aligned,  '\n",
      "                  '\\\\nEngines of logic map the mind.  \\\\nSignals transition, '\n",
      "                  \"clear and free.']\",\n",
      " 'max_drafts': 2,\n",
      " 'poem_subject': 'state machines',\n",
      " 'poem_types': ['sonnet', 'limerick', 'haiku', 'acrostic'],\n",
      " 'proposals': [['In myriad states, a cyclic dance unfolds,  \\n'\n",
      "                'Upon the stage of logic, firm and clear.  \\n'\n",
      "                'An automaton with structured bounds it holds,  \\n'\n",
      "                'Designed by minds, both intricate and near.  \\n'\n",
      "                '\\n'\n",
      "                'With each transition, patterns trace the flow,  \\n'\n",
      "                'A pulse of truth propels the forward sweep.  \\n'\n",
      "                'Conditions mark the paths, both high and low,  \\n'\n",
      "                'As inputs carve formations, wide and deep.  \\n'\n",
      "                '\\n'\n",
      "                'Deterministic threads, like textiles spun,  \\n'\n",
      "                'Weave tapestries of boundless, coded choice.  \\n'\n",
      "                'A binary ballet that shies from none,  \\n'\n",
      "                'Yet whispers softly with a silent voice.  \\n'\n",
      "                '\\n'\n",
      "                'In finite bounds, infinite patterns weave,  \\n'\n",
      "                'Order from chaos, logic born to leave.  ',\n",
      "                'In a realm where nodes intertwine,  \\n'\n",
      "                \"Dwells the dream of a state machine's line.  \\n\"\n",
      "                'With transitions so neat,  \\n'\n",
      "                'A puzzle complete,  \\n'\n",
      "                'It dances through states, a design.',\n",
      "                'Beads of states align,  \\n'\n",
      "                'Silent rhythms weave their dance—  \\n'\n",
      "                \"Logic's steady hand.\",\n",
      "                'Silent gears in whispered dance engage,  \\n'\n",
      "                'Transitions weave through every stage.  \\n'\n",
      "                'Abstract notions structured with flair,  \\n'\n",
      "                'Tapestries of circuits, precise and rare.  \\n'\n",
      "                'Each decision flows with calculated grace,  \\n'\n",
      "                '\\n'\n",
      "                'Machines hum softly, hidden in embrace.  \\n'\n",
      "                'As states shift, each holds its refrain,  \\n'\n",
      "                'Choreographed steps in methodical chain.  \\n'\n",
      "                'Harmony thrives in digital cadence set,  \\n'\n",
      "                'In vivid lines of binary duet.  \\n'\n",
      "                'Nodes in purpose, connections aligned,  \\n'\n",
      "                'Engines of logic map the mind.  \\n'\n",
      "                'Signals transition, clear and free.']]}\n"
     ]
    }
   ],
   "source": [
    "ray.init(ignore_reinit_error=True)\n",
    "act, _, state = app.run(\n",
    "    halt_after=[\"final_results\"],\n",
    "    inputs={\n",
    "        \"max_drafts\": 2,\n",
    "        \"poem_types\": [\n",
    "            \"sonnet\",\n",
    "            \"limerick\",\n",
    "            \"haiku\",\n",
    "            \"acrostic\",\n",
    "        ],\n",
    "        \"poem_subject\": \"state machines\",\n",
    "    },\n",
    ")\n",
    "pprint.pprint(state.get_all())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c0bf20f4-63b6-4fc1-a7f0-bdbae62f6635",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
